const mysql = require('mysql');
const pool = mysql.createPool({
    connectionLimit: 10,
    host: 'localhost',
    port: 3306,
    user: 'root',
    password: 'admin123',
    database: 'dx1008'
});

// 导出 配置好的 连接池对象！
// export default pool
module.exports = pool

// 1.执行 查询
// pool.query('SELECT id,name,age FROM dogs where delflag=0', function (error, results, fields) {
//     if (error) throw error;
//     console.log(results);
// });

// // 2.执行 新增
// let newDog1 = { name: '小白兔112', age: 1 }
// // let strAsql = `insert into dogs (name,age) values ("${newDog1.name}",${newDog1.age})`
// let strAsql2 = `insert into dogs set ?`

// pool.query(strAsql2, newDog1, function (error, results, fields) {
//     if (error) throw error;
//     console.log(results);
// });

// // 3.执行 删除
// pool.query('delete from dogs where id =4', function (error, results, fields) {
//     if (error) throw error;
//     console.log(results);
// });

// 4.执行 修改
// let newDog = { id: 11, name: '小花很美丽3', age: 3 }
// let strSql1 = `update dogs set name="${newDog.name}",age=${newDog.age} where id = ${newDog.id};`
// let strSql2 = `update dogs set name=?,age=? where id = ?;`

// pool.query(strSql2, [newDog.name, newDog.age, newDog.id], function (error, results, fields) {
//     if (error) throw error;
//     console.log(results);
// });

// 优化语法：
// let strSql3 = `update dogs set ? where id = ?;`
// // -> `update dogs set id=11,name='小花很美丽3',age=3 where id = 11;`

// pool.query(strSql3, [newDog, newDog.id], function (error, results, fields) {
//     if (error) throw error;
//     console.log(results);
// });